package com.api.gbhg.lbj.action;

import com.api.gbhg.lbj.dao.ReimbursementDao;
import com.api.gbhg.zengping.common.CommonDao;
import com.api.gbhg.zengping.dao.PropertyManagementCostDao;
import com.api.gbhg.zengping.web.PropertyManagementAction;
import com.sap.db.util.StringUtil;
import com.weaver.general.BaseBean;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;
import weaver.workflow.workflow.WorkflowVersion;

import java.util.HashMap;
import java.util.Map;

import static com.api.gbhg.lbj.tool.Tools.getsfkzsydwbxed;

/**
 * @author lbj
 * @version 1.0
 * @description: TODO 主表冻结
 * @date 2021/3/10 18:47
 */
public class FreezemaintableAction implements Action {
    private static final Logger logger = LoggerFactory.getLogger("gbhg");
    @Override
    public String execute(RequestInfo requestInfo) {
        logger.info("进入主表冻结金额FreezemaintableAction---");
        ReimbursementDao rei =new ReimbursementDao();
        //请求id
        String requestid = requestInfo.getRequestid();
        //标题
        String description = requestInfo.getDescription();
        //获取表名
        String billTableName = requestInfo.getRequestManager().getBillTableName();
        //流程id
        String workflowid = requestInfo.getWorkflowid();
        RecordSet rs = CommonDao.selectByRequestId(new RecordSet(), billTableName, requestid);
        Map<String,String> list=new HashMap<>();
        RecordSetTrans rst = new RecordSetTrans();
        rst.setAutoCommit(false);
        if (rs.next()) {
            list = new HashMap<>();
            String ysxm = rs.getString("ysxm");
            String bxje = rs.getString("bxje");
            String bmjjzcfl = rs.getString("bmjjzcfl");
            String bccxjkje = rs.getString("bccxjkje");
            String tybxje = rs.getString("tybxje");
            if (StringUtils.isBlank(bccxjkje)){
                bccxjkje="0.00";
            }
            if (StringUtils.isBlank(tybxje)){
                tybxje="0.00";
            }
            String bxr = rs.getString("bxr");
            logger.info("预算项目:" + ysxm + "申请报销金额:" + bxje + "部门经济支出分类:" + bmjjzcfl);
            list.put("workflowid", workflowid);
            list.put("requestid",requestid);//报销流程id
            list.put("lcjlid", ysxm);//预算项目
            list.put("money", bxje);//申请报销金额
            list.put("bmjjzcfl", bmjjzcfl);//部门经济支出分类
            list.put("cxje", bccxjkje);//冲销金额
            list.put("tybxje", tybxje);//同意报销金额
            list.put("bxr", bxr);//报销人

            Double sqbxje = rs.getDouble("bxje");
            Double cxje = rs.getDouble("bccxjkje");
            Double ce = sqbxje-cxje;
            RecordSet rs2 = new RecordSet();
            rs2.execute(" SELECT xmmc,syed ,syed-("+ce+") AS chae FROM uf_jingfei WHERE lcjlid="+ysxm+" AND bmzcjjfl="+bmjjzcfl);
            rs2.next();
            Double chae = rs2.getDouble("chae");
            if(chae<0){
                requestInfo.getRequestManager().setMessagecontent("报销预算项目:"+rs2.getString("xmmc")+"的经济分类["+bmjjzcfl+"]剩余额度:"+rs2.getString("syed")+" 不足以报销"+sqbxje);
                requestInfo.getRequestManager().setMessageid("1");
                return FAILURE_AND_CONTINUE;

            }

            PropertyManagementAction.addDetail(list, description, workflowid);//添加主表报销明细
            boolean b = true;
            boolean b1 = true;
            try {
                b = rei.queryMainJingfei(rst,list,false,0);
                RecordSet rs1 = getsfkzsydwbxed(ysxm);
                rs1.next();
                String sfkzsydwbxed = rs1.getString("sfkzsydwbxed");
                String lcid = rs1.getString("lcid");
                BaseBean baseBean=new BaseBean();
                String BZ18Workflowid = WorkflowVersion.getActiveVersionWFID(baseBean.getPropValue("FormationWorkflowid", "BZ18Workflowid"));//读取配置文件
                logger.info("是否控制使用单位报销额度:"+sfkzsydwbxed+",workflowid:"+lcid+",BZ18Workflowid:"+BZ18Workflowid);
                if (StringUtils.isNotBlank(sfkzsydwbxed)&&sfkzsydwbxed.equals("1")&&!lcid.equals(BZ18Workflowid)){
                    b1 = rei.userQuotaFreezeDeduction(rst,list, true);
                }
                logger.info("b:"+ b+",b1:"+b1);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (!b||!b1) {
                rst.rollback();
                //删除报销记录
                rs.execute("DELETE FROM uf_bxsjjl WHERE lcid="+requestid);
                rs.execute("DELETE FROM uf_bxjl_kj WHERE lcid="+requestid);
                rs.execute("DELETE FROM uf_bxjl_kj_dt1 WHERE lcid="+requestid);
                rs.execute("DELETE FROM uf_bxjl_kj_dt2 WHERE lcid="+requestid);
                logger.info("=====================冻结错误=======================");
                requestInfo.getRequestManager().setMessagecontent("冻结失败");
                requestInfo.getRequestManager().setMessageid("1");
                return FAILURE_AND_CONTINUE;
            }
        }
        rst.commit();
        return Action.SUCCESS;
    }
}
